package com.example.myvideo.View.impl;

import com.example.myvideo.contract.AuthView;
import com.example.myvideo.database.DBHelper;
import com.example.myvideo.model.AuthModel;
import com.example.myvideo.presenter.AuthPresenter;

public class AuthPresenterImpl implements AuthPresenter {
    private AuthView view;
    private AuthModel model;
    private DBHelper dbHelper;

    public AuthPresenterImpl(AuthView view, AuthModel model, DBHelper dbHelper) {
        this.view = view;
        this.model = model;
        this.dbHelper = dbHelper;
    }

    @Override
    public void login(String username, String password) {
        view.showLoading();
        if (model.authenticateUser(username, password)) {
            view.navigateToHomeScreen();
        } else {
            view.showErrorMessage("Invalid username or password");
        }
        view.hideLoading();
    }

    @Override
    public void register(String username, String password, String avatarPath) {
        view.showLoading();
        if (!model.checkUserExists(username)) {
            long userId = model.addUser(username, password, avatarPath);
            if (userId > 0) {
                view.showRegistrationSuccess();
                view.navigateToHomeScreen();
            } else {
                view.showErrorMessage("Registration failed");
            }
        } else {
            view.showUsernameError();
        }
        view.hideLoading();
    }
}